// 文章列表信息存储
import {defineStore} from "pinia";
import {api} from "@/utils/api";

export const useArticleListStore = defineStore('articleList', {
    persist: true,
    state: () => ({
        articleCategory: '',
        currentPage: 1,
        pageSize: 10,
        currentArticleIndex: 0,
        articleList: [] as Article[],
        articleListForPrevNext: [[], [], []] as ArticleRecord[][], // [前一页数据, 当前页数据, 后一页数据]
    }),
    actions: {
        async loadArticles(page: number) {
            // 在每次加载新页前清除当前文章列表
            this.articleList = [];

            const response = await api.getNewsCenterPage(this.articleCategory, page, this.pageSize);
            if (response && response.code === 1) {
                const articles: Article[] = response.data.content.map((article: Article) => ({
                    id: article.id,
                    title: article.title,
                    createBy: article.createBy,
                    updateBy: article.updateBy,
                    createTime: article.createTime,
                    updateTime: article.updateTime,
                    type: article.type,
                    type2: article.type2,
                    pic: article.pic,
                    pdfFileName: article.pdfFileName,
                    details: article.details,
                }));
                this.articleList = articles;

                // 更新 articleListForPrevNext 中的“当前页”数据
                this.articleListForPrevNext[1] = articles; // 设置当前页
                this.currentPage = page;
            }
        },
        async setCurrentArticleIndex(articleId: number) {
            let index = this.articleList.findIndex((article) => article.id === articleId);
            if (index === -1) {
                // 尝试在前一页或下一页中查找
                const prevPageResponse = await api.getNewsCenterPage(this.articleCategory, this.currentPage - 1, this.pageSize);
                const nextPageResponse = await api.getNewsCenterPage(this.articleCategory, this.currentPage + 1, this.pageSize);

                if (prevPageResponse.code === 1) {
                    const prevArticles = prevPageResponse.data.content;
                    index = prevArticles.findIndex((article: Article) => article.id === articleId);
                    if (index !== -1) {
                        this.articleList = prevArticles;
                        this.currentPage -= 1;
                        this.currentArticleIndex = index;
                        return;
                    }
                }

                if (nextPageResponse.code === 1) {
                    const nextArticles = nextPageResponse.data.content;
                    index = nextArticles.findIndex((article: Article) => article.id === articleId);
                    if (index !== -1) {
                        this.articleList = nextArticles;
                        this.currentPage += 1;
                        this.currentArticleIndex = index;
                        return;
                    }
                }

                console.error("Article not found in adjacent pages");
            } else {
                this.currentArticleIndex = index;
            }
        }
        ,
        async loadPrevNextPages() {
            if (this.currentArticleIndex === 0 && this.currentPage > 1) {
                // 加载上一页数据
                const response = await api.getNewsCenterPage(this.articleCategory, this.currentPage - 1, this.pageSize);
                if (response && response.code === 1) {
                    this.articleListForPrevNext[0] = response.data.content.map((article: Article) => ({
                        id: article.id,
                        title: article.title,
                    })); // 设置上一页
                }
            }

            if (this.currentArticleIndex === this.articleList.length - 1 && this.articleList.length === this.pageSize) {
                // 加载下一页数据
                const response = await api.getNewsCenterPage(this.articleCategory, this.currentPage + 1, this.pageSize);
                if (response && response.code === 1) {
                    this.articleListForPrevNext[2] = response.data.content.map((article: Article) => ({
                        id: article.id,
                        title: article.title,
                    })); // 设置下一页
                }
            }
        },
        async peekPreviousArticle(): Promise<ArticleRecord | null> {
            // 临时索引和页码
            let tempIndex = this.currentArticleIndex;
            let tempPage = this.currentPage;

            if (tempIndex === 0 && tempPage > 1) {
                // 加载上一页的数据
                const response = await api.getNewsCenterPage(this.articleCategory, tempPage - 1, this.pageSize);
                if (response && response.code === 1) {
                    const articles: Article[] = response.data.content;
                    return articles[articles.length - 1] || null;
                }
            } else if (tempIndex > 0) {
                // 返回当前列表中的前一篇文章
                return this.articleList[tempIndex - 1] || null;
            }
            return null;
        },

        async peekNextArticle(): Promise<ArticleRecord | null> {
            let tempIndex = this.currentArticleIndex;
            let tempPage = this.currentPage;

            if (tempIndex === this.articleList.length - 1) {
                // 加载下一页的数据
                const response = await api.getNewsCenterPage(this.articleCategory, tempPage + 1, this.pageSize);
                if (response && response.code === 1 && response.data.content.length > 0) {
                    const articles: Article[] = response.data.content;
                    return articles[0] || null;
                }
            } else if (tempIndex < this.articleList.length - 1) {
                // 返回当前列表中的下一篇文章
                return this.articleList[tempIndex + 1] || null;
            }
            return null;
        },
        async getPreviousArticle(): Promise<ArticleRecord | null> {
            if (this.currentArticleIndex === 0 && this.currentPage > 1) {
                // 加载上一页数据并更新索引
                await this.loadArticles(this.currentPage - 1);
                this.currentArticleIndex = this.articleList.length - 1;
                return this.articleList[this.currentArticleIndex] || null;
            } else if (this.currentArticleIndex > 0) {
                this.currentArticleIndex -= 1;
                return this.articleList[this.currentArticleIndex];
            }
            return null;
        },
        async getNextArticle(): Promise<ArticleRecord | null> {
            if (this.currentArticleIndex === this.articleList.length - 1) {
                // 加载下一页数据并更新索引
                await this.loadArticles(this.currentPage + 1);
                this.currentArticleIndex = 0;
                return this.articleList[this.currentArticleIndex] || null;
            } else if (this.currentArticleIndex < this.articleList.length - 1) {
                this.currentArticleIndex += 1;
                return this.articleList[this.currentArticleIndex];
            }
            return null;
        },
    },
});